#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   tftp_stop.sh
# Version:    1.0
# Date:       2013/05/15
# Author:      huijing.hei
# Email:       huijing.hei@cs2c.com.cn
# Summary:     02系统基本功能-04网络服务-02tftp-03停止服务
# Notes:       depends on tftp_start_s1.sh
# Copyright:    China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/05/15
#             - Start tftp service.
# ----------------------------------------------------------------------
# include lib files
if [ -z "$SFROOT" ]
then
    CMDLIB=./
    SFROOT=./
else
    CMDLIB=${SFROOT}/testcases/Network_Service
fi

. ${SFROOT}/lib/Echo.sh
. ${CMDLIB}/lib/Net.sh
. ${SFROOT}/lib/XmlParse.sh

. ${CMDLIB}/setup.sh

# get RHOST, RUSERNAME, PASSWORD from config.xml
DATA_FILE="${SFROOT}/testcases/Network_Service/config.xml"
[ -e $DATA_FILE ] || { echo "$DATA_FILE not find, test end."; exit 1; }

XmlParse ${DATA_FILE}

GetValue REHOST ReHost
GetValue RUSERNAME UserName
GetValue PASSWORD RootPasswd

export RHOST=${REHOST}
export RUSERNAME=${RUSERNAME}
export PASSWORD=${PASSWORD}


setup_env || { echo "setup env failed, test end."; exit 1; }

TFTP_CONF="/etc/xinetd.d/tftp"
${RSH} -n -l root $RHOST "[ -f $TFTP_CONF.bak ] && mv -f $TFTP_CONF.bak $TFTP_CONF"
EchoResult "back tftp.bak conf."

# restart tftp on remote
serv=xinetd
${RSH} -n -l root $RHOST "command -v systemctl && /bin/systemctl restart ${serv}.service || /sbin/service ${serv} restart"

${RSH} -n -l root $RHOST "ps aux | grep in.tftpd | grep -v grep && killall in.tftpd"


sleep 3
RC=`${RSH} -n -l root $RHOST "netstat -ap | grep tftp"`
echo ${RC} | grep tftp
if [ $? -ne 0 ];then
    echo "tftp stop successed."
else
    echo "tftp stop failed."
    exit 1
fi

exit 0
